package hkust.ee.fyp.drawer;

import hkust.ee.fyp.R;
import hkust.ee.fyp.drawer.StickerAdapter.StickerAdapterCallback;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class AddSticker extends Activity implements StickerAdapterCallback{

	private StickerAdapterCallback m_callback = this;
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
	    super.onCreate(savedInstanceState); 
	 
	    setContentView(R.layout.add_sticker); 
	 
	    GridView g = (GridView) findViewById(R.id.sticker_grid_view);
	    StickerAdapter adapter = new StickerAdapter(this);
	    adapter.setCallback( m_callback);
	    g.setAdapter(adapter);
	}

	@Override
	public void onSelectSticker(StickerAdapter sender) {

	    Intent result = new Intent();
		Bundle bundle = new Bundle();
		
		bundle.putInt( "sticker_resource_id", sender.getSelectedItem());
		result.putExtras( bundle);
		
	    setResult( Activity.RESULT_OK, result);
	    finish();
	}
}

class StickerAdapter extends BaseAdapter implements OnClickListener {

    private Context mContext;
    private StickerAdapterCallback m_callback = null;
    private int selectedIndex = -1;
    
    private Integer[] mThumbIds = {
            R.drawable.sticker_artbook,
            R.drawable.sticker_box,
            R.drawable.sticker_boy,
            R.drawable.sticker_camera,
            R.drawable.sticker_car,
            R.drawable.sticker_cat,
            R.drawable.sticker_caution,
            R.drawable.sticker_dog,
            R.drawable.sticker_elephant,
            R.drawable.sticker_empty_box,
            R.drawable.sticker_flower,
            R.drawable.sticker_girl,
            R.drawable.sticker_home,
            R.drawable.sticker_music_player,
            R.drawable.sticker_musical_bag,
            R.drawable.sticker_musical_note,
            R.drawable.sticker_photos,
            R.drawable.sticker_red_cross,
            R.drawable.sticker_star,
            R.drawable.sticker_thumbdrive,          
            };
    
    public StickerAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public int getItemIndex(ImageView imageView)
    {
    	return (Integer) imageView.getTag();
    }
    
    public int getSelectedItem()
    {
    	return mThumbIds[selectedIndex];
    }
    
	public void setCallback(StickerAdapterCallback callback) {
		m_callback = callback;
	}

    @Override
    public View getView( int position, View convertView, ViewGroup parent) { 
        final ImageView imageView; 
        if (convertView == null) { 
            imageView = new ImageView(mContext); 
            imageView.setLayoutParams(new GridView.LayoutParams(128, 128));
            imageView.setAdjustViewBounds(false); 
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            
        } else { 
            imageView = (ImageView) convertView; 
        } 
 
        imageView.setImageResource(mThumbIds[position]); 
        imageView.setTag(position);
        imageView.setOnClickListener(this);
        return imageView; 
    }
    
	public interface StickerAdapterCallback {
		public void onSelectSticker(StickerAdapter sender);
	}
	
	public void onClick(View v) {
		if(v instanceof ImageView){
			ImageView imageView = (ImageView) v;
			selectedIndex = getItemIndex(imageView);
			m_callback.onSelectSticker(StickerAdapter.this);
		}
	}
}